package com.jp.knowledge.my.util;

import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract.*;
import android.provider.ContactsContract.CommonDataKinds.*;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author larson
 *
 */
public class ContactUtil {

    private static ContactUtil contactUtil = new ContactUtil();
    private ContactUtil(){}
    public static ContactUtil getContactUtil(){
        return contactUtil;
    }

    // ContactsContract.Contacts.CONTENT_URI= content://com.android.contacts/contacts;
    // ContactsContract.Data.CONTENT_URI = content://com.android.contacts/data;
    public List<ContactModel> getContactInfo(Context context) {

        List<ContactModel> contactModels = new ArrayList<>();

        Cursor cursor = context.getContentResolver().query(Data.CONTENT_URI,
                null, null, null, Data.RAW_CONTACT_ID);

        while (cursor.moveToNext()) {
            ContactModel contactModel = null;

            int contactId = cursor.getInt(cursor.getColumnIndex(Data.RAW_CONTACT_ID));
            for(int i = 0; i < contactModels.size(); i++){
                if(contactModels.get(i).getContactId() == contactId){
                    contactModel = contactModels.get(i);
                    break;
                }
            }
            if(contactModel == null){
                contactModel = new ContactModel();
                contactModel.setContactId(cursor.getInt(cursor.getColumnIndex(Data.RAW_CONTACT_ID)));
            }

            String mimeType = cursor.getString(cursor.getColumnIndex(Data.MIMETYPE));

            if (StructuredName.CONTENT_ITEM_TYPE.equals(mimeType)) {
                NameModel nameModel = new NameModel();
                nameModel.setMimetype(mimeType);
                nameModel.setDisplayName(cursor.getString(cursor.getColumnIndex(StructuredName.DISPLAY_NAME)));
                nameModel.setPrefix(cursor.getString(cursor.getColumnIndex(StructuredName.PREFIX)));
                nameModel.setFirstName(cursor.getString(cursor.getColumnIndex(StructuredName.FAMILY_NAME)));
                nameModel.setMiddleName(cursor.getString(cursor.getColumnIndex(StructuredName.MIDDLE_NAME)));
                nameModel.setLastname(cursor.getString(cursor.getColumnIndex(StructuredName.GIVEN_NAME)));
                nameModel.setSuffix(cursor.getString(cursor.getColumnIndex(StructuredName.SUFFIX)));
                nameModel.setPhoneticFirstName(cursor.getString(cursor.getColumnIndex(StructuredName.PHONETIC_FAMILY_NAME)));
                nameModel.setPhoneticMiddleName(cursor.getString(cursor.getColumnIndex(StructuredName.PHONETIC_MIDDLE_NAME)));
                nameModel.setPhoneticLastName(cursor.getString(cursor.getColumnIndex(StructuredName.PHONETIC_GIVEN_NAME)));
                if(contactModel.getNameModels() == null)
                    contactModel.setNameModels(new ArrayList<NameModel>());
                contactModel.getNameModels().add(nameModel);
            } else if (Phone.CONTENT_ITEM_TYPE.equals(mimeType)) {
                PhoneModel phoneModel = new PhoneModel();
                phoneModel.setMimetype(mimeType);
                phoneModel.setPhoneType(cursor.getInt(cursor.getColumnIndex(Phone.TYPE)));
                String phoneNumber = cursor.getString(cursor.getColumnIndex(Phone.NUMBER));
                phoneModel.setPhoneNumber(formatPhone(phoneNumber));
                phoneModel.setMobileEmail(cursor.getString(cursor.getColumnIndex(Email.DATA)));
                if(contactModel.getPhoneModels() == null)
                    contactModel.setPhoneModels(new ArrayList<PhoneModel>());
                contactModel.getPhoneModels().add(phoneModel);
            } else if(Event.CONTENT_ITEM_TYPE.equals(mimeType)){
                EventModel eventModel = new EventModel();
                eventModel.setMimetype(mimeType);
                eventModel.setEventType(cursor.getInt(cursor.getColumnIndex(Event.TYPE)));
                eventModel.setEventDate(cursor.getString(cursor.getColumnIndex(Event.START_DATE)));
                if(contactModel.getEventModels() == null)
                    contactModel.setEventModels(new ArrayList<EventModel>());
                contactModel.getEventModels().add(eventModel);
            } else if(Im.CONTENT_ITEM_TYPE.equals(mimeType)){
                ProtocalModel protocalModel = new ProtocalModel();
                protocalModel.setMimetype(mimeType);
                protocalModel.setProtocal(cursor.getInt(cursor.getColumnIndex(Im.PROTOCOL)));
                protocalModel.setData(cursor.getString(cursor.getColumnIndex(Im.DATA)));
                if(contactModel.getProtocalModels() == null)
                    contactModel.setProtocalModels(new ArrayList<ProtocalModel>());
                contactModel.getProtocalModels().add(protocalModel);
            } else if(Note.CONTENT_ITEM_TYPE.equals(mimeType)){
                RemarkModel remarkModel = new RemarkModel();
                remarkModel.setMimetype(mimeType);
                remarkModel.setRemark(cursor.getString(cursor.getColumnIndex(Note.NOTE)));
                if(contactModel.getRemarkModels() == null)
                    contactModel.setRemarkModels(new ArrayList<RemarkModel>());
                contactModel.getRemarkModels().add(remarkModel);
            } else if(Nickname.CONTENT_ITEM_TYPE.equals(mimeType)){
                NickNameModel nickNameModel = new NickNameModel();
                nickNameModel.setMimetype(mimeType);
                nickNameModel.setNickName(cursor.getString(cursor.getColumnIndex(Nickname.NAME)));
                if(contactModel.getNickNameModels() == null)
                    contactModel.setNickNameModels(new ArrayList<NickNameModel>());
                contactModel.getNickNameModels().add(nickNameModel);
            } else if(Organization.CONTENT_ITEM_TYPE.equals(mimeType)){
                OrgModel orgModel = new OrgModel();
                orgModel.setMimetype(mimeType);
                orgModel.setOrgType(cursor.getInt(cursor.getColumnIndex(Organization.TYPE)));
                orgModel.setCompany(cursor.getString(cursor.getColumnIndex(Organization.COMPANY)));
                orgModel.setJobTitle(cursor.getString(cursor.getColumnIndex(Organization.TITLE)));
                orgModel.setDepartment(cursor.getString(cursor.getColumnIndex(Organization.DEPARTMENT)));
                if(contactModel.getOrgModels() == null)
                    contactModel.setOrgModels(new ArrayList<OrgModel>());
                contactModel.getOrgModels().add(orgModel);
            } else if(Website.CONTENT_ITEM_TYPE.equals(mimeType)){
                WebModel webModel = new WebModel();
                webModel.setMimetype(mimeType);
                webModel.setWebType(cursor.getInt(cursor.getColumnIndex(Website.TYPE)));
                webModel.setUrl(Website.URL);
                if(contactModel.getWebModels() == null)
                    contactModel.setWebModels(new ArrayList<WebModel>());
                contactModel.getWebModels().add(webModel);
            } else if(StructuredPostal.CONTENT_ITEM_TYPE.equals(mimeType)){
                PostalModel postalModel = new PostalModel();
                postalModel.setMimetype(mimeType);
                postalModel.setPostalType(cursor.getInt(cursor.getColumnIndex(StructuredPostal.TYPE)));
                postalModel.setStreet(cursor.getString(cursor.getColumnIndex(StructuredPostal.STREET)));
                postalModel.setCity(cursor.getString(cursor.getColumnIndex(StructuredPostal.CITY)));
                postalModel.setPoBox(cursor.getString(cursor.getColumnIndex(StructuredPostal.POBOX)));
                postalModel.setArea(cursor.getString(cursor.getColumnIndex(StructuredPostal.NEIGHBORHOOD)));
                postalModel.setRegion(cursor.getString(cursor.getColumnIndex(StructuredPostal.REGION)));
                postalModel.setPostCode(cursor.getString(cursor.getColumnIndex(StructuredPostal.POSTCODE)));
                postalModel.setCountry(cursor.getString(cursor.getColumnIndex(StructuredPostal.COUNTRY)));
                if(contactModel.getPostalModels() == null)
                    contactModel.setPostalModels(new ArrayList<PostalModel>());
                contactModel.getPostalModels().add(postalModel);
            }

            contactModels.add(contactModel);
        }
        cursor.close();
        return contactModels;
    }

    /**
     * 格式化电话号码 去掉空格  中划线等
     * @param phoneNumber
     * @return
     */
    private String formatPhone(String phoneNumber) {
        if (phoneNumber != null) {
            // 有的通讯录格式为135-1568-1234
            phoneNumber = phoneNumber.replaceAll("-", "");
            // 有的通讯录格式中 - 变成了空格
            phoneNumber = phoneNumber.replaceAll(" ", "");
            if (phoneNumber.startsWith("+86"))
                phoneNumber = phoneNumber.substring(3);
        } else {
            phoneNumber = "";
        }
        // 有些手机号码格式 86后有空格
        return phoneNumber.trim();
    }

    public class ContactModel{
        private int contactId;

        /**
         * Data.MIMETYPE                        mimetype 类型，扩展数据都在该类型中
         *      StructuredName.CONTENT_ITEM_TYPE     名字信息
         *      PhoneModel.CONTENT_ITEM_TYPE              号码信息
         *      EventModel.CONTENT_ITEM_TYPE              日期信息
         *      Im.CONTENT_ITEM_TYPE                 即时通信信息
         *      Note.CONTENT_ITEM_TYPE               备注信息
         *      Organization.CONTENT_ITEM_TYPE       组织信息
         *      Website.CONTENT_ITEM_TYPE            网站信息
         *      StructuredPostal.CONTENT_ITEM_TYPE   地址信息
         * */
//        private String mimetype;        // mimetype 类型，扩展数据都在该类型中

        private List<NameModel> nameModels;        // 名字信息
        private List<RemarkModel> remarkModels;   // 标记信息
        private List<NickNameModel> nickNameModels;// 昵称信息
        private List<PhoneModel> phoneModels;     // 号码信息
        private List<EventModel> eventModels;     // 事件信息
        private List<ProtocalModel> protocalModels;// 即时通信信息
        private List<OrgModel> orgModels;         // 组织信息
        private List<WebModel> webModels;         // 网站信息
        private List<PostalModel> postalModels;   // 通信地址信息

        public int getContactId() {
            return contactId;
        }

        public void setContactId(int contactId) {
            this.contactId = contactId;
        }

        public List<NameModel> getNameModels() {
            return nameModels;
        }

        public void setNameModels(List<NameModel> nameModels) {
            this.nameModels = nameModels;
        }

        public List<RemarkModel> getRemarkModels() {
            return remarkModels;
        }

        public void setRemarkModels(List<RemarkModel> remarkModels) {
            this.remarkModels = remarkModels;
        }

        public List<NickNameModel> getNickNameModels() {
            return nickNameModels;
        }

        public void setNickNameModels(List<NickNameModel> nickNameModels) {
            this.nickNameModels = nickNameModels;
        }

        public List<PhoneModel> getPhoneModels() {
            return phoneModels;
        }

        public void setPhoneModels(List<PhoneModel> phoneModels) {
            this.phoneModels = phoneModels;
        }

        public List<EventModel> getEventModels() {
            return eventModels;
        }

        public void setEventModels(List<EventModel> eventModels) {
            this.eventModels = eventModels;
        }

        public List<ProtocalModel> getProtocalModels() {
            return protocalModels;
        }

        public void setProtocalModels(List<ProtocalModel> protocalModels) {
            this.protocalModels = protocalModels;
        }

        public List<OrgModel> getOrgModels() {
            return orgModels;
        }

        public void setOrgModels(List<OrgModel> orgModels) {
            this.orgModels = orgModels;
        }

        public List<WebModel> getWebModels() {
            return webModels;
        }

        public void setWebModels(List<WebModel> webModels) {
            this.webModels = webModels;
        }

        public List<PostalModel> getPostalModels() {
            return postalModels;
        }

        public void setPostalModels(List<PostalModel> postalModels) {
            this.postalModels = postalModels;
        }
    }

    public class NameModel{
        /**
         * StructuredName.CONTENT_ITEM_TYPE     名字信息
         * */
        private String mimetype;        // mimetype 类型，扩展数据都在该类型中
        private String displayName;
        private String prefix;
        private String firstName;
        private String middleName;
        private String lastname;
        private String suffix;
        private String phoneticFirstName;
        private String phoneticMiddleName;
        private String phoneticLastName;

        public String getMimetype() {
            return mimetype;
        }

        public void setMimetype(String mimetype) {
            this.mimetype = mimetype;
        }

        public String getDisplayName() {
            return displayName;
        }

        public void setDisplayName(String displayName) {
            this.displayName = displayName;
        }

        public String getPrefix() {
            return prefix;
        }

        public void setPrefix(String prefix) {
            this.prefix = prefix;
        }

        public String getFirstName() {
            return firstName;
        }

        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }

        public String getMiddleName() {
            return middleName;
        }

        public void setMiddleName(String middleName) {
            this.middleName = middleName;
        }

        public String getLastname() {
            return lastname;
        }

        public void setLastname(String lastname) {
            this.lastname = lastname;
        }

        public String getSuffix() {
            return suffix;
        }

        public void setSuffix(String suffix) {
            this.suffix = suffix;
        }

        public String getPhoneticFirstName() {
            return phoneticFirstName;
        }

        public void setPhoneticFirstName(String phoneticFirstName) {
            this.phoneticFirstName = phoneticFirstName;
        }

        public String getPhoneticMiddleName() {
            return phoneticMiddleName;
        }

        public void setPhoneticMiddleName(String phoneticMiddleName) {
            this.phoneticMiddleName = phoneticMiddleName;
        }

        public String getPhoneticLastName() {
            return phoneticLastName;
        }

        public void setPhoneticLastName(String phoneticLastName) {
            this.phoneticLastName = phoneticLastName;
        }
    }

    public class RemarkModel{
        /**
         * Note.CONTENT_ITEM_TYPE               备注信息
         * */
        private String mimetype;        // mimetype 类型，扩展数据都在该类型中
        private String remark;          // 备注信息

        public String getMimetype() {
            return mimetype;
        }

        public void setMimetype(String mimetype) {
            this.mimetype = mimetype;
        }

        public String getRemark() {
            return remark;
        }

        public void setRemark(String remark) {
            this.remark = remark;
        }
    }

    public class NickNameModel{
        private String mimetype;        // mimetype 类型，扩展数据都在该类型中
        private String nickName;        // 昵称

        public String getMimetype() {
            return mimetype;
        }

        public void setMimetype(String mimetype) {
            this.mimetype = mimetype;
        }

        public String getNickName() {
            return nickName;
        }

        public void setNickName(String nickName) {
            this.nickName = nickName;
        }
    }

    public class PhoneModel {
        /**
         * PhoneModel.CONTENT_ITEM_TYPE              号码信息
         *
         * PhoneModel.TYPE                           号码类型
         *      PhoneModel.TYPE_MOBILE                   手机
         *      PhoneModel.TYPE_HOME                     住宅电话
         *      PhoneModel.TYPE_WORK                     单位电话
         *      PhoneModel.TYPE_FAX_WORK                 单位传真
         *      PhoneModel.TYPE_FAX_HOME                 住宅传真
         *      PhoneModel.TYPE_PAGER                    寻呼机
         *      PhoneModel.TYPE_CALLBACK                 回拨号码
         *      PhoneModel.TYPE_COMPANY_MAIN             公司总机
         *      PhoneModel.TYPE_CAR                      车载电话
         *      PhoneModel.TYPE_ISDN                     ISDN
         *      PhoneModel.TYPE_MAIN                     总机
         *      PhoneModel.TYPE_RADIO                    无线装置
         *      PhoneModel.TYPE_TELEX                    电报
         *      PhoneModel.TYPE_TTY_TDD                  TTY_TDD
         *      PhoneModel.TYPE_WORK_MOBILE              单位手机
         *      PhoneModel.TYPE_WORK_PAGER               单位寻呼机
         *      PhoneModel.TYPE_ASSISTANT                助理
         *      PhoneModel.TYPE_MMS                      彩信
         * */
        private String mimetype;        // mimetype 类型，扩展数据都在该类型中
        private int phoneType;         // 号码类型
        private String phoneNumber;    // 号码
        private String mobileEmail;    // 邮件

        public String getMimetype() {
            return mimetype;
        }

        public void setMimetype(String mimetype) {
            this.mimetype = mimetype;
        }

        public int getPhoneType() {
            return phoneType;
        }

        public void setPhoneType(int phoneType) {
            this.phoneType = phoneType;
        }

        public String getPhoneNumber() {
            return phoneNumber;
        }

        public void setPhoneNumber(String phoneNumber) {
            this.phoneNumber = phoneNumber;
        }

        public String getMobileEmail() {
            return mobileEmail;
        }

        public void setMobileEmail(String mobileEmail) {
            this.mobileEmail = mobileEmail;
        }
    }

    public class EventModel {
        /**
         * EventModel.CONTENT_ITEM_TYPE              日期信息
         *
         * EventModel.CONTENT_ITEM_TYPE              日期类型
         *      EventModel.TYPE_BIRTHDAY                 生日
         *      EventModel.TYPE_ANNIVERSARY              周年纪念日
         * */
        private String mimetype;        // mimetype 类型，扩展数据都在该类型中
        private int eventType;         // 日期类型
        private String eventDate;      // 日期值

        public String getMimetype() {
            return mimetype;
        }

        public void setMimetype(String mimetype) {
            this.mimetype = mimetype;
        }

        public int getEventType() {
            return eventType;
        }

        public void setEventType(int eventType) {
            this.eventType = eventType;
        }

        public String getEventDate() {
            return eventDate;
        }

        public void setEventDate(String eventDate) {
            this.eventDate = eventDate;
        }
    }

    public class ProtocalModel {
        /**
         * Im.CONTENT_ITEM_TYPE                 即时通信信息
         *
         * Im.PROTOCOL                          即时消息类型
         *      Im.TYPE_CUSTOM                      自定义
         *      Im.PROTOCOL_MSN                     MSM
         *      Im.PROTOCOL_QQ                      QQ
         * */
        private String mimetype;        // mimetype 类型，扩展数据都在该类型中
        private int protocal;          // 即时通信类型
        private String data;            // 即时通信

        public String getMimetype() {
            return mimetype;
        }

        public void setMimetype(String mimetype) {
            this.mimetype = mimetype;
        }

        public int getProtocal() {
            return protocal;
        }

        public void setProtocal(int protocal) {
            this.protocal = protocal;
        }

        public String getData() {
            return data;
        }

        public void setData(String data) {
            this.data = data;
        }
    }

    public class OrgModel {
        /**
         * Organization.CONTENT_ITEM_TYPE       组织信息
         *
         * Organization.TYPE                    组织类型
         *      Organization.TYPE_CUSTOM        自定义
         * */
        private String mimetype;        // mimetype 类型，扩展数据都在该类型中
        private int orgType;            // 组织类型
        private String company;         // 公司
        private String jobTitle;        // 职位
        private String department;      // 部门

        public String getDepartment() {
            return department;
        }

        public void setDepartment(String department) {
            this.department = department;
        }

        public String getJobTitle() {
            return jobTitle;
        }

        public void setJobTitle(String jobTitle) {
            this.jobTitle = jobTitle;
        }

        public int getOrgType() {
            return orgType;
        }

        public void setOrgType(int orgType) {
            this.orgType = orgType;
        }

        public String getCompany() {
            return company;
        }

        public void setCompany(String company) {
            this.company = company;
        }

        public String getMimetype() {
            return mimetype;
        }

        public void setMimetype(String mimetype) {
            this.mimetype = mimetype;
        }
    }

    public class WebModel {
        /**
         * Website.CONTENT_ITEM_TYPE            网站信息
         *
         * Website.TYPE                         网站类型
         *      Website.TYPE_CUSTOM                 自定义
         *      Website.TYPE_HOME                   主页
         *      Website.TYPE_HOMEPAGE               个人主页
         *      Website.TYPE_WORK                   工作主页
         * */
        private String mimetype;        // mimetype 类型，扩展数据都在该类型中
        private int webType;            // 网站类型
        private String url;              // 网站链接

        public String getMimetype() {
            return mimetype;
        }

        public void setMimetype(String mimetype) {
            this.mimetype = mimetype;
        }

        public int getWebType() {
            return webType;
        }

        public void setWebType(int webType) {
            this.webType = webType;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }
    }

    public class PostalModel {
        /**
         * StructuredPostal.CONTENT_ITEM_TYPE   通信地址信息
         *
         * StructuredPostal.TYPE                通信地址类型
         *      StructuredPostal.TYPE_WORK          工作地址
         *      StructuredPostal.TYPE_HOME          住宅地址
         *      StructuredPostal.TYPE_OTHER         其他通信地址
         * */
        private String mimetype;        // mimetype 类型，扩展数据都在该类型中
        private int postalType;         // 地址类型
        private String street;          // 街道
        private String city;            // 城市
        private String poBox;           // 邮件地址
        private String area;            // 地区
        private String region;          // STATE
        private String postCode;        // ZIP
        private String country;         // 国家

        public String getMimetype() {
            return mimetype;
        }

        public void setMimetype(String mimetype) {
            this.mimetype = mimetype;
        }

        public int getPostalType() {
            return postalType;
        }

        public void setPostalType(int postalType) {
            this.postalType = postalType;
        }

        public String getStreet() {
            return street;
        }

        public void setStreet(String street) {
            this.street = street;
        }

        public String getCity() {
            return city;
        }

        public void setCity(String city) {
            this.city = city;
        }

        public String getPoBox() {
            return poBox;
        }

        public void setPoBox(String poBox) {
            this.poBox = poBox;
        }

        public String getArea() {
            return area;
        }

        public void setArea(String area) {
            this.area = area;
        }

        public String getRegion() {
            return region;
        }

        public void setRegion(String region) {
            this.region = region;
        }

        public String getPostCode() {
            return postCode;
        }

        public void setPostCode(String postCode) {
            this.postCode = postCode;
        }

        public String getCountry() {
            return country;
        }

        public void setCountry(String country) {
            this.country = country;
        }
    }
}